package com.dinadi.yidi.utils.algorithms;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.SecureRandom;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

/**
 * des加密工具类
 * Created by jason on 2017/4/9.
 */
public class DesUtil {
    private final static String DES = "DES";
    private final static String ENCODE = "utf-8";
    private final static String defaultKey = "yidiyidi";

    public static void main(String[] args) throws Exception {
        String data = "123";
        // System.err.println(encrypt(data, key));
        // System.err.println(decrypt(encrypt(data, key), key));
        //System.out.println(encrypt(data));
        //System.out.println(decrypt(encrypt(data)));

        String msg = "2+OHUcGNa6qntroXqf6Hd2rAgVHNr6yu07EBUZLkzLnWYcVedOnlwDXPiW8HlZ81rAElN3/0ibR7ngCRwjCJNZ66YzgHOvgkYxH/biS9b//heg5qDTBuCWCkKmMBAxpeSEiwYkFtC0VgWFakYF/6DYaNJRZMHg7BcXVPmnpz/P/bfMmyS9XJrwvgjICe6Edhf0e7evwW5NfoIbSwuJodb48VQIKYWb+5/SG8NbOkhTY0gRl8pa/E740OfbuzE2Ce/j4q5+zIimG2SlyksmrZt9ByXkJ2m95dFryJZo0SxVu9T4ln+M08tsFrWxrbn1QASENjWsSt5f7HqCAm5ARwNGwk9Mu8EY9ClF5fKPjxh4ZtU91Fhi6OHz0GGQ3uvoVRhpeIEdk50TGvZZaEcwCDZzMzcxbY5LD7YRPA/1TLLHmQQui4byAQsgLwgeIQOYRT5gA59o0xYfryAUT/B3NORNc89XOgRENEr0dQRn/f5qk2BvSXb025zqRCyTvAxTe310E/pY8q1tobGb7lgr7txSkPQF1OSBFgOg4hgWe4bvH1sW6OBjCq2/zOLldmJi95QV6iKP/SeRgQ6E7G/2CmtgcmSmNlOEkBim1muMYrhP+8assaIYVo/lZp6/9+gXWDXtQK7YDAUpU1OqIUs6bFH4JCpO8mf0haPZHPmAhe6EHpknbNeQ3s/5Vg/fyZX8eANwzv6Nx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6ag+yrbQZexgIzsV7aIMTu46BNtrGCrdmoNRZJ5rsHZSGCxeEU5a6Ntodtl4Q9+6BfpPvFjxeERGU0BdqLgYhno5LU+BvXPTEb1hUNHgCWBKZf2roHjxo1mXt5j4XtQK7YDAUpWvmd3Tj6+esdCwCv1AGl5DeuY0823ONeRb0YDBy0kcNZVg/fyZX8eANwzv6NxJkhXSAYeI0kEm4GrCkE6I1ZqeOPmMWL6NyxqG13SVK4gBNzxcWNl+3ozP1BZctcIA9eYn5YKxrORZ7EMeBcNFEnyPtorq/JUJ3WFWWQVXqvL/z/nLN1/QTehgMPl+n2AX8sBrJO6DVYSwgx0rS38uDXrb0BY8EUWBCZ9sr118sbCbJAvNFLfCTTNVheSU3AhKDg7M+76nynlnaPDJWVSI3xSSWATAz7FpYIb9X/HGM/ENqw6hPWHN8SPzI44F28f+2MdvB4WQS8tYSa3Jfi7+jI4/aSJ5hy+xOWyqfod/UrXCkqGKloly2T7c";

        System.out.println(decrypt(msg));

    }

    /**
     * 使用 默认key 加密
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午02:46:43
     */
    public static String encrypt(String data) throws Exception {
        byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * 使用 默认key 解密
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午02:49:52
     */
    public static String decrypt(String data) throws IOException, Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }

    /**
     * Description 根据键值进行加密
     *
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * Description 根据键值进行解密
     *
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, key.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }

    /**
     * Description 根据键值进行加密
     *
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

    /**
     * Description 根据键值进行解密
     *
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }
}
